Skip to content

chore: Apply clang-format to entire codebase#2820

Draft
githubawn wants to merge 2 commits into
TheSuperHackers:mainfrom
githubawn:chore/clang-format-codebase
Draft

chore: Apply clang-format to entire codebase#2820
githubawn wants to merge 2 commits into
TheSuperHackers:mainfrom
githubawn:chore/clang-format-codebase

Conversation

@githubawn

@githubawn githubawn commented Jun 21, 2026

Copy link
Copy Markdown

This PR applies a repository-wide mechanical reformatting across all project source code using clang-format.

This is the successor to the original formatting attempt in PR #2638 (submitted by @DevGeniusCode). Following up on the community poll in Discord ("Apply clang-format on all code", which passed with 53%), this PR establishes the formatting baseline. Because the output is 100% automated, if conflicts arise from other PRs landing before this one, regenerating the diff is trivial (re-run the formatter and force-push).

A follow-up PR will add a .git-blame-ignore-revs file containing the commit hash of this formatting pass so that git blame (both locally and on GitHub) automatically skips over the formatting changes and preserves the historical authorship of every line.


What Changed vs PR #2638

Three config adjustments have been applied based on review feedback and legacy toolchain compatibility:

-UseTab: AlignWithSpaces
+UseTab: ForIndentation
+SpacesInAngles: Leave
+Cpp11BracedListStyle: false
  • UseTab: ForIndentation: Fixes the tab-space rendering mismatch in preprocessor block comments on GitHub (where GitHub defaults to tab=8, causing space-aligned block comments inside tab-indented preprocessor blocks to look misaligned). This resolves the issue without having to touch the source file comments themselves.
  • SpacesInAngles: Leave: Retains existing spacing inside template brackets (e.g., keeping > > rather than collapsing them to >>), which is required for legacy C++98 / VC6 compiler compatibility.
  • Cpp11BracedListStyle: false: Prevents the formatter from collapsing braced initializer lists to C++11 single-line style, maintaining Allman brace layout consistency.

All other open review items from PR #2638 have been resolved, are hardcoded clang-format parser behaviors (such as the Doxygen Javadoc star alignment), or have since landed on main.


Key Legacy & Toolchain Compatibility Settings

To ensure the codebase continues to compile cleanly on both modern toolchains (VS2022) and the legacy target toolchain (Visual C++ 6), the following settings are established in .clang-format:

  1. VC6 Template Compatibility (SpacesInAngles: Leave):
    Prevents clang-format from collapsing nested template arguments (e.g., std::vector<std::vector<int> > to >>). VC6 and older compilers misparse >> as a right-shift operator. PR chore: Prevent conflict between clang-format and pre-C++11 nested template parsing #2760 (and its closed predecessor PR refactor: Extract nested templates into typedefs for legacy compatibility #2642) already resolved the instances where it was collapsed, and this setting ensures it stays that way.
  2. VC6 Braced Initializer Compatibility (Cpp11BracedListStyle: false):
    Prevents formatting braced-init-lists in C++11 style, which causes parsing issues on legacy compilers.
  3. MSVC Inline Assembly Safety:
    clang-format has known parser conflicts with MSVC-style inline assembly blocks when not enclosed in curly braces (which can cause the formatter to join assembly lines and break compilation). These have been pre-emptively wrapped in curly braces (__asm { ... }) in main via PR chore: Prevent conflict between clang-format and MSVC by wrapping inline assembly blocks in curly braces #2811, making the formatter pass entirely safe.
  4. Macro-Split Assignments Resolved:
    Problems regarding assignment formatting split across macros have been pre-emptively addressed by PR refactor: Eliminate macro-split assignments #2641.

Scope & Merging Strategy

The PR includes the full codebase diff across all source files.

  • Open to Review Rounds: This PR is open to further review rounds. If any formatting settings need adjustments, the codebase can be formatted again and the branch force-pushed.
  • Trivial Merge Conflict Resolution: If another pull request gets merged into main before this one, resolving conflicts is trivial: we simply re-run the formatter locally on top of the updated main branch and force-push.

Zero-Trust Verification

Reviewers can verify that this PR is 100% mechanically generated with no manual edits by running the following steps directly from main without checking out this branch:

Step 1 — Download the .clang-format file from this PR

curl -sL https://raw.githubusercontent.com/TheSuperHackers/GeneralsGameCode/refs/pull/[PR_NUMBER]/head/.clang-format -o .clang-format

Step 2 — Run the formatter locally

# Bash
find Core Generals GeneralsMD scripts resources/gitinfo \( -name "*.cpp" -o -name "*.h" -o -name "*.inl" \) | xargs clang-format -i
# PowerShell
Get-ChildItem -Path Core, Generals, GeneralsMD, scripts, resources/gitinfo -Include *.cpp, *.h, *.inl -Recurse | ForEach-Object { clang-format -i $_.FullName }

Step 3 — Compare local changes with the PR diff

git diff > local.diff && gh pr diff [PR_NUMBER] > pr.diff && diff local.diff pr.diff

Expected result: The diff output must be completely empty. If silent, it guarantees the PR contains only tool-generated formatting changes.


How to Migrate Existing Feature Branches

If you have an active feature branch developed before this bulk formatting pass, you can migrate it to the new formatted main branch cleanly without manual conflict resolution by letting Git ignore whitespace during the rebase, and then formatting your changes at the end:

Step 1 — Checkout your feature branch

git checkout my-feature-branch

Step 2 — Pull the .clang-format config from main

git checkout main -- .clang-format

Step 3 — Rebase onto main ignoring whitespace changes

git rebase main -Xignore-all-space

(Git's -Xignore-all-space option tells the merge engine to ignore whitespace differences when replaying your commits on top of the formatted main, allowing your logical changes to merge cleanly).

Step 4 — Run the formatter to format your edits

Once the rebase completes, format the codebase to clean up the lines you edited:

# Bash
find Core Generals GeneralsMD scripts resources/gitinfo -type f \( -name "*.cpp" -o -name "*.h" -o -name "*.inl" \) -print0 | xargs -0 clang-format -i
# PowerShell
Get-ChildItem -Path Core, Generals, GeneralsMD, scripts, resources/gitinfo -Include *.cpp, *.h, *.inl -Recurse | ForEach-Object { clang-format -i $_.FullName }

Step 5 — Commit the formatting cleanup

git add -A
git commit -m "chore: apply clang-format"

@greptile-apps

greptile-apps Bot commented Jun 21, 2026

Copy link
Copy Markdown

Too many files changed for review. (3000 files found, 500 file limit)

@DevGeniusCode

Copy link
Copy Markdown
-AlignEscapedNewlines: Left

If I remember correctly, we intentionally chose left alignment with a 4-character spacing.

@githubawn

Copy link
Copy Markdown
Author
-AlignEscapedNewlines: Left

If I remember correctly, we intentionally chose left alignment with a 4-character spacing.

Thanks. Updated description, accidentally pasted older version. This line wasn't changed.

@xezon

xezon commented Jun 22, 2026

Copy link
Copy Markdown

What is the correct strategy to migrate old branches to new main after the formatting was merged into main?

@githubawn

githubawn commented Jun 22, 2026

Copy link
Copy Markdown
Author

One needs to switch to their branch, rebase, clang-format and commit.

Step 1 — Checkout your feature branch

git checkout my-feature-branch

Step 2 — Pull the .clang-format config from main

git checkout main -- .clang-format

Step 3 — Rebase onto main ignoring whitespace changes

git rebase main -Xignore-all-space

(Git's -Xignore-all-space option tells the merge engine to ignore whitespace differences when replaying your commits on top of the formatted main, allowing your logical changes to merge cleanly).

Step 4 — Run the formatter to format your edits

Once the rebase completes, format the codebase to clean up the lines you edited:

# Bash
find Core Generals GeneralsMD scripts resources/gitinfo -type f \( -name "*.cpp" -o -name "*.h" -o -name "*.inl" \) -print0 | xargs -0 clang-format -i
# PowerShell
Get-ChildItem -Path Core, Generals, GeneralsMD, scripts, resources/gitinfo -Include *.cpp, *.h, *.inl -Recurse | ForEach-Object { clang-format -i $_.FullName }

Updated the description, current one was indeed a wrong order. Tested in another fork.

@xezon

xezon commented Jun 22, 2026

Copy link
Copy Markdown

Did you test it? Does it work? This does not look as if it would work. I would expect the formatting needs to be merged into each commit of the branch?

@DevGeniusCode

Copy link
Copy Markdown
-UseTab: AlignWithSpaces
+UseTab: ForIndentation
+SpacesInAngles: Leave
+Cpp11BracedListStyle: false

Can you plz add short code examples before and after the changes?

@githubawn githubawn marked this pull request as draft June 23, 2026 12:49
@githubawn

Copy link
Copy Markdown
Author

I'll update the description soon with more validating, newer guide, and code examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants